﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Attendences.Framework.BusinessLogic;
using Attendences.Framework.Entity;

public partial class Page_UserAttendList : BaseAuthenticationPage
{
    public string SearchClauseList
    {
        get
        {
            if (ViewState["SearchClauseList"] == null)
                ViewState["SearchClauseList"] = "1=1";
            return ViewState["SearchClauseList"].ToString();
        }
        set
        {
            ViewState["SearchClauseList"] = value;
        }
    }

    public string SearchClauseItem
    {
        get
        {
            if (ViewState["SearchClauseItem"] == null)
                ViewState["SearchClauseItem"] = "1=1";
            return ViewState["SearchClauseItem"].ToString();
        }
        set
        {
            ViewState["SearchClauseItem"] = value;
        }
    }

    protected DataTable AttendList
    {
        get
        {
            if (ViewState["AttendList"] == null)
                ViewState["AttendList"] = new DataTable();
            return ViewState["AttendList"] as DataTable;
        }
        set
        {
            ViewState["AttendList"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "值班列表";

        this.tbSearchDate.Attributes.Add("readonly", "true");

        if (!IsPostBack)
        {
            this.Order.OrderColumn = "CreateTime";
            this.Order.OrderDirection = "DESC";
            BindData();
        }
    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    private void BindData()
    {
        int recordCount = 0;
        SearchClauseList+=string.Format("AND {0}=N'{1}'",View_Attend_User.SQLCOL_USERID,CurrentUser.ID);
        AttendList = BLAttendences.GetViewAttendList(SearchClauseList, Pager.PageIndex, Pager.PageSize, string.Format("{0} {1}", this.Order.OrderColumn, this.Order.OrderDirection), ref recordCount);
        Pager.RecordCount = recordCount;
        AttendList.Columns.Add("AttendDate", typeof(string));
        AttendList.Columns.Add("AttendDateAlt", typeof(string));
        AttendList.Columns.Add("AttendTime", typeof(string));
        foreach (DataRow dr in AttendList.Rows)
        {
            DataTable dtAttendDate = BLAttendences.GetAttendenceItem(string.Format("{0} = '{1}'", AttendenceItem.SQLCOL_ATTENDENCEID, dr["ID"]));
            string strAttendDate = string.Empty;
            string strAttendTime = string.Empty;
            for (int i = 0; i < dtAttendDate.Rows.Count; i++)
            {
                strAttendDate += Convert.ToDateTime(dtAttendDate.Rows[i]["AttendDate"]).ToString("yyyy-MM-dd");
                strAttendTime += Convert.ToDateTime(dtAttendDate.Rows[i][AttendenceItem.SQLCOL_ATTENDTIMESTART]).ToString("HH:mm至") + Convert.ToDateTime(dtAttendDate.Rows[i][AttendenceItem.SQLCOL_ATTENDTIMEEND]).ToString("HH:mm");
                if (i < dtAttendDate.Rows.Count - 1)
                {
                    strAttendDate += "<br />";
                    strAttendTime += "<br />";
                }
            }
            dr["AttendDate"] = strAttendDate;
            dr["AttendTime"] = strAttendTime;
            dr["AttendDateAlt"] = strAttendDate.Replace("<br />", "&#13;").Replace("&nbsp;", " ");
        }
        rptAttendences.DataSource = AttendList;
        rptAttendences.DataBind();
    }

    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        SearchClauseItem = "1=1";
        if (tbSearchDate.Text != string.Empty)
            SearchClauseItem += string.Format("AND {0} = '{1}'", AttendenceItem.SQLCOL_ATTENDDATE, tbSearchDate.Text);
        if (tbSearchTime.Text != string.Empty)
            SearchClauseItem += string.Format(" AND {0}=N'{1}'", AttendenceItem.SQLCOL_ATTENDTIMESTART, tbSearchTime.Text.Replace("'", "''").Replace("%", "[%]"));
        if (tbSearchUserDisplayName.Text != string.Empty)
            SearchClauseItem += string.Format(" AND {0} LIKE N'%{1}%'", AttendenceDate.SQLCOL_USERDISPLAYNAME, tbSearchUserDisplayName.Text.Replace("'", "''").Replace("%", "[%]"));

        SearchClauseList = "(1=0";
        DataTable searchAttendItem = new DataTable();
        searchAttendItem = BLAttendences.GetAttendenceItem(SearchClauseItem);
        for (int i = 0; i < searchAttendItem.Rows.Count; i++)
        {
            //drItem["AttendenceID"] = drItem[AttendenceItem.SQLCOL_ATTENDENCEID];
            int id = (int)searchAttendItem.Rows[i][AttendenceItem.SQLCOL_ATTENDENCEID];
            SearchClauseList += string.Format("OR {0}={1} ", Attendence.SQLCOL_ID, id);
        }
        SearchClauseList +=")";
        
        tbSearchDate.Text = string.Empty;
        tbSearchTime.Text = string.Empty;
        tbSearchUserDisplayName.Text = string.Empty;
        BindData();
    }
}

